package com.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * @author U 自定义侧拉菜单
 */
public class SlidingMenu extends ViewGroup {

	private View mLeftView;
	private View mContentView;
	private int mLeftWidth;
	private float mDownX;
	private Scroller mScroller;// Scroller:collect the data,用来做动画
	private float mDownX1;
	// * 1 写类继承ViewGroup
	// * 2 onMeasure
	// * 1 给自己设置大小
	// * 2 给孩子测量
	// * 3 onLayout
	// * 1 给孩子布局
	// * 4 左边布局(ScrollView)和内容布局
	// * 5 跟用户进行交互（onTouchEvent）
	private float mDownY1;

	public SlidingMenu(Context context) {
		this(context, null);
	}

	public SlidingMenu(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mScroller = new Scroller(context);
	}

	// 把xml解析成View完成之后，并且所有孩子已经被添加了
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mLeftView = getChildAt(0);
		mContentView = getChildAt(1);
		// 获取左边的值
		LayoutParams params = mLeftView.getLayoutParams();
		mLeftWidth = params.width;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mDownX1 = ev.getX();
			mDownY1 = ev.getY();
			mDownX=0;
			break;
		case MotionEvent.ACTION_MOVE:
			float moveX1 = ev.getX();
			float moveY1 = ev.getY();
			if (Math.abs(moveX1 - mDownX1) > Math.abs(moveY1 - mDownY1)) {// 左右滑动
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:

			break;

		default:
			break;
		}
		return super.onInterceptTouchEvent(ev);
	}

	// 给自己设置大小，给孩子测量
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		// 给 内容View进行测量
		mContentView.measure(widthMeasureSpec, heightMeasureSpec);

		// 给左边View 进程测量
		int leftMeasureSpec = MeasureSpec.makeMeasureSpec(mLeftWidth, MeasureSpec.EXACTLY);
		mLeftView.measure(leftMeasureSpec, heightMeasureSpec);
		// MeasureSpec
		// 1 MeasureSpec 它封装布局请求，从父亲传给孩子
		// 2 每个MeasureSpec代表是width和height
		// 3 一个MeasureSpec包含大小和模式
		// UNSPECIFIED：一般是系统用，或者不知父亲情况下用来占位
		// EXACTLY：match_parent或者10dp
		// AT_MOST：wrap_content
		// size+mode:int 32位 头两位存储mode,后面三十位存储大小
		// Log.d("heima11", "width:"+width+";height:"+height);
		// int mode = MeasureSpec.getMode(widthMeasureSpec);
		// if(mode==MeasureSpec.EXACTLY){
		// Log.d("heima11", "EXACTLY");
		// }
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		setMeasuredDimension(width, height);
	}

	// 给孩子布局
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {

		// 先给内容View进行布局
		mContentView.layout(0, 0, mContentView.getMeasuredWidth(), mContentView.getMeasuredHeight());

		// 给左边的View进行布局
		mLeftView.layout(-mLeftWidth, 0, 0, mLeftView.getMeasuredHeight());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mDownX = event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			float moveX = event.getX();
			int dx = (int) (mDownX - moveX - 0.5f);
			// 左右边界的处理
			// 右边界的处理
			int scrollX = getScrollX() + dx;
			if (scrollX > 0) {
				scrollTo(0, 0);
			} else if (scrollX < -mLeftWidth) {
				scrollTo(-mLeftWidth, 0);
			} else {
				// 移动方法
				scrollBy(dx, 0);
			}

			// scrollTo(x, y);
			mDownX = moveX;
			break;
		case MotionEvent.ACTION_UP:
			// 抬起进行一半的判断
			int halfLeftWidth = -mLeftWidth / 2;
			int sX = getScrollX();
			boolean isPastHalf = sX <= halfLeftWidth;
			showMenu(isPastHalf);
			break;

		default:
			break;
		}
		return true;
	}

	private void showMenu(boolean isPastHalf) {
		if (isPastHalf) {
			// scrollTo(-mLeftWidth,0);
			int startX = getScrollX();
			int endX = -mLeftWidth;
			int dx = endX - startX;
			mScroller.startScroll(startX, 0, dx, 0);// 收集数据
			invalidate();
		} else {
			// scrollTo(0,0);
			int startX = getScrollX();
			int endX = 0;
			int dx = endX - startX;
			mScroller.startScroll(startX, 0, dx, 0);// 收集数据
			invalidate();
		}
	}

	@Override
	public void computeScroll() {
		super.computeScroll();

		/**
		 * Call this when you want to know the new location. If it returns true,
		 * the animation is not yet finished.
		 */
		if (mScroller.computeScrollOffset()) {
			int currX = mScroller.getCurrX();
			scrollTo(currX, 0);
			invalidate();
		}
	}

}
